﻿// (C) Copyright 2025 by  
//

using System.Collections.Generic;
using AutoCAD_CSharp_plug_in3;
using AutoCAD_CSharp_plug_in3.Chap01;
using AutoCAD_CSharp_plug_in3.Chap02;
using AutoCAD_CSharp_plug_in3.Chap03;
using AutoCAD_CSharp_plug_in3.Chap04;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using static Autodesk.AutoCAD.ApplicationServices.Application;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
using Exception = System.Exception;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(MainCommands))]

namespace AutoCAD_CSharp_plug_in3
{
    public class MainCommands
    {
        [CommandMethod("MainCommands", "ShowBreakDialog", "显示管线打断工具", CommandFlags.Modal)]
        public void ShowBreakDialog()
        {
            try
            {
                var form = new PipeSplitForm();
                ShowModalDialog(form);
            }
            catch (Exception ex)
            {
                var doc = Application.DocumentManager.MdiActiveDocument;
                if (doc != null) doc.Editor.WriteMessage($"\n错误: {ex.Message}");
            }
        }

        [CommandMethod("ig541Test")]
        public void Ig541Test()
        {
            Ig541Action.Ig541Test();
        }

        [CommandMethod("ig541Rel")]
        public void Ig541Rel()
        {
            Ig541Action.Ig541Rel();
        }

        [CommandMethod("ebl")]
        public void Ebl()
        {
            EconomicBottleLayout.Handle();
        }


        [CommandMethod("dob")]
        public void InsertDorBlock()
        {
            DorBlock.InsertDorBlock();
        }

        [CommandMethod("doi")]
        public void ImportBlock()
        {
            BlockAction.ImportBlock();
        }

        [CommandMethod("dor")]
        public void RemoveBlock()
        {
            BlockAction.RemoveBlock();
        }

        [CommandMethod("msc")]
        public static void MyBreakLine()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityOptions opt1 = new PromptEntityOptions("\nselect a line:");
            opt1.SetRejectMessage("\nerror!");
            opt1.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult res1 = ed.GetEntity(opt1);
            if (res1.Status == PromptStatus.OK)
            {
                PromptPointOptions opt2 = new PromptPointOptions("\nselect second point:");
                opt2.AllowNone = true;
                PromptPointResult res2 = ed.GetPoint(opt2);
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    Polyline l = (Polyline)tr.GetObject(res1.ObjectId, OpenMode.ForRead);
                    List<double> pars = new List<double>();
                    Point3d pt1 = l.GetClosestPointTo(res1.PickedPoint, false);
                    pars.Add(l.GetParameterAtPoint(pt1));
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
                    if (res2.Status == PromptStatus.OK)
                    {
                        Point3d pt2 = l.GetClosestPointTo(res2.Value, false);
                        pars.Add(l.GetParameterAtPoint(pt2));
                        pars.Sort();
                        DBObjectCollection objs = l.GetSplitCurves(new DoubleCollection(pars.ToArray()));
                        foreach (Polyline ll in objs)
                        {
                            if ((ll.StartPoint != pt1 && ll.StartPoint != pt2) ^
                                (ll.EndPoint != pt1 && ll.EndPoint != pt2))
                            {
                                btr.AppendEntity(ll);
                                tr.AddNewlyCreatedDBObject(ll, true);
                            }
                        }
                    }
                    else
                    {
                        DBObjectCollection objs = l.GetSplitCurves(new DoubleCollection(pars.ToArray()));
                        foreach (Polyline ll in objs)
                        {
                            btr.AppendEntity(ll);
                            tr.AddNewlyCreatedDBObject(ll, true);
                        }
                    }

                    l.UpgradeOpen();
                    l.Erase();
                    tr.Commit();
                }
            }
        }
    }
}